<?php

namespace falcon5\inc\WPProviders;

use falcon5\inc\Base\ServiceProviderInterface;

class MonoLogger implements ServiceProviderInterface
{

    public function register()
    {
        if (falcon5_log_dir_is_writable(true)) {
//            add_action( 'init', array( $this, 'register_logger' ) );
            $this->register_logger();
        }

    }

    public function register_logger()
    {
        $logger = falcon5_log();

        // 设置错误处理函数
        set_error_handler(function ($type, $msg, $file, $line) use ($logger) {
            $logger->error("Error: $msg in $file on line $line", ['errno' => $type]);
            return false;
        });

        // 设置异常处理函数
        set_exception_handler(function ($exception) use ($logger) {
            $logger->error("Exception: " . $exception->getMessage(), [
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => $exception->getTrace(),
            ]);
        });

    }
}